'''
Created on Jul 16, 2009

@author: xin
@author: Mikael Rousson
'''
import numpy

def find_significant_coeff(samples, kpercent):
    """ Finds the indices of the most significant coefficients in vector or 
    matrix "samples", based on the L2-norm of each row of this last "samples" 
    must be a column vector or a matrix containing a row for each sample 
    kpercent represents the number of samples to be deleted (the first k-th 
    percentile in abs value).
    
    @return Returns a column vector of indices.
    """
    Nx, dim = samples.shape

    if kpercent > 0:
        kp = kpercent / 100
        n = numpy.floor(kp * Nx)
        if dim == 1 :
            samp_norm = numpy.abs(samples)
        elif dim > 1:
            samp_norm = (numpy.sum(samples ** 2, 1)).T
        else:
            print "Not valid dimension for vector samples"

        B = numpy.sort(samp_norm)
        IND = numpy.argsort(samp_norm)
        # selection based on threshold
        threshold = B[int(n)]
        significant = []
        for i in range(len(B)):
            if B[i] > threshold:
                significant.append(IND[i])
    else:
        significant = numpy.arange(1, Nx + 1).T

    return significant
